﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.Components;
using Aliyeye.Car.Net.UI.Common;
using Aliyeye.Car.Net.UI.PrintExport;
using Aliyeye.Car.Net.UI.PopupDialog;

namespace Aliyeye.Car.Net.UI.StatisticsModel
{
    public partial class StatisticsByCustomer : Aliyeye.Car.Net.UI.BaseForm
    {
        private string strSearchShortDriver = string.Empty;
        private string strSearchLongDriver = string.Empty;
        private string strSearchShortNotDriver = string.Empty;
        private string strSearchLongNotDriver = string.Empty;
        private DataSet dsAllShortDriver = null;
        private DataSet dsAllLongDriver = null;
        private DataSet dsAllShortNotDriver = null;
        private DataSet dsAllLongNotDriver = null;

        public StatisticsByCustomer()
        {
            InitializeComponent();
            DateTime dtNow = DateTime.Now;
            DateTime dt = new DateTime(dtNow.Year, dtNow.Month, 1);

            dtstarttimeShortDrive.Value = dt;
            dtendtimeShortDrive.Value = dtNow;

            dtstarttimeLongDrive.Value = dt;
            dtendtimeLongDrive.Value = dtNow;

            dtstarttimeShortNotDrive.Value = dt;
            dtendtimeShortNotDrive.Value = dtNow;

            dtstarttimeLongNotDrive.Value = dt;
            dtendtimeLongNotDrive.Value = dtNow;
        }

        private void StatisticsByCustomer_Load(object sender, EventArgs e)
        {
            //this.pagerShortDriver.PageCurrent = 1;
            //this.pagerShortDriver.Bind();
            //GetAllInfoShortDriverByCondistion();

            PageCommon.Notify("请使用搜寻功能查找数据");
        }


        #region 短租带驾

        //pagerShortDriver 
        private int pagerShortDriver_EventPaging(EventPagingArg e)
        {
            return dgvBindShortDriver();
        }

        private int dgvBindShortDriver()
        {
            string strWhere = " Reserve.Type='4' and Reserve.OrderSataus='true'";

            PageData pageData = new PageData(this, pagerShortDriver, this.overlay);

            pageData.TableName = "Reserve left join Customer on Customer.Id=Reserve.CustId left join Car on Car.Id=Reserve.CarId left join Accounts_Users on Accounts_Users.UserID=Reserve.UserId left join Accounts_Users DriverUser on DriverUser.UserId=Car.UserId";
            pageData.PrimaryKey = "Reserve.Id";
            pageData.OrderStr = "Reserve.InpuTime desc";
            pageData.PageIndex = this.pagerShortDriver.PageCurrent;
            pageData.PageSize = this.pagerShortDriver.PageSize;
            pageData.QueryCondition = strWhere + strSearchShortDriver.ToString();
            pageData.QueryFieldName = "DriverUser.TrueName as DriverName, Car.Code, Car.Brand, car.carType, car.TimeoutPrie, car.OvertimePrie, Reserve.ReceivedAmount-Reserve.TotalAmount as yingshou , Reserve.*,Customer.Name as customerName, Accounts_Users.TrueName as inputUserName";

            dgvInfoShortDriver.AutoGenerateColumns = false;
            DataSet ds = pageData.QueryDataTable();
            dgvInfoShortDriver.DataSource = this.pagerShortDriver.bindingSource;
            return pageData.TotalCount;
        }

        private void GetAllInfoShortDriverByCondistion()
        {
            //string _strSql = "1=1 ";
            string strWhere = "";
            strWhere = " where Reserve.Type='4' and Reserve.OrderSataus='true'" + strSearchShortDriver;
            string strOrderBy = " order by Reserve.InpuTime desc";
            string strSelect = "select DriverUser.TrueName as DriverName, Car.Code, Car.Brand, car.carType, car.TimeoutPrie, car.OvertimePrie , Reserve.ReceivedAmount-Reserve.TotalAmount as yingshou, Reserve.*  ,Customer.Name as customerName, Accounts_Users.TrueName as inputUserName from Reserve left join Customer on Customer.Id=Reserve.CustId left join Car on Car.Id=Reserve.CarId left join Accounts_Users on Accounts_Users.UserID=Reserve.UserId left join Accounts_Users DriverUser on DriverUser.UserId=Car.UserId ";
            string strSql = strSelect + strWhere.ToString() + strOrderBy;

            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            CarSvc.SoapHeaderClass sopaHeader = new CarSvc.SoapHeaderClass();

            ServiceManipulator.SafeAsyncExecution<CarSvc.CarServiceSoapClient>(
                proxy => proxy.GetAllListForSearchCondistion(sopaHeader, strSql),
                result =>
                {
                    if (result.Error == null)
                    {
                        dsAllShortDriver = (DataSet)result.Result;
                        decimal sumShiShou = 0;
                        decimal sumWeiShou = 0;
                        decimal yingshou = 0;

                        if (dsAllShortDriver != null && dsAllShortDriver.Tables[0].Rows.Count > 0)
                        {
                            foreach (DataRow dr in dsAllShortDriver.Tables[0].Rows)
                            {
                                yingshou += Convert.ToDecimal(dr["yingshou"]);
                                sumShiShou += Convert.ToDecimal(dr["ReceivedAmount"]);
                                sumWeiShou += Convert.ToDecimal(dr["TotalAmount"]);
                            }
                        }
                        lblYingShouShortDriver.Text = yingshou.ToString() + " 元";
                        lblShiShouShortDirver.Text = sumShiShou.ToString() + " 元";
                        lblWeiShouShortDriver.Text = sumWeiShou.ToString() + " 元";
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message);
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );
        }

        private void dgvInfoShortDriver_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvInfoShortDriver.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvInfoShortDriver.Rows.Count; )
                {
                    this.dgvInfoShortDriver.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }

        private void dgvInfoShortDriver_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            e.CellStyle.WrapMode = DataGridViewTriState.True;

            if (this.dgvInfoShortDriver.Columns[e.ColumnIndex].Name == "日期")
            {
                if (e.Value != null)
                {
                    e.Value = Convert.ToDateTime(e.Value).Date.ToString("yyyy-MM-dd");
                }
            }

            if (this.dgvInfoShortDriver.Columns[e.ColumnIndex].Name == "时间")
            {
                if (e.Value != null)
                {
                    e.Value = Convert.ToDateTime(e.Value).Hour.ToString() + ":" + Convert.ToDateTime(e.Value).Minute.ToString();
                }
            }
        }

        //查询-短租带驾
        private void btnSearchShortDriver_Click(object sender, EventArgs e)
        {
            string strWhere = "";
            strWhere += " and Reserve.InpuTime BETWEEN '" + dtstarttimeShortDrive.Value + "' AND '" + dtendtimeShortDrive.Value + "'";

            if (!string.IsNullOrEmpty(txtUserNameShortDriver.Text))
            {
                strWhere += " And Customer.Name like '%" + txtUserNameShortDriver.Text + "%'";
            }
            strSearchShortDriver = strWhere;

            this.pagerShortDriver.Bind();
            GetAllInfoShortDriverByCondistion();
        }

        //导出-短租带驾
        private void btnExportShortDriver_Click(object sender, EventArgs e)
        {
            try
            {
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string filePath = saveFileDialog.FileName;
                    ExcelHelper excelHelper = new ExcelHelper();
                    string msg = "";
                    if (excelHelper.StatisticsByCustomerShortDriverExport(dsAllShortDriver, filePath, ref msg))
                    {
                        PageCommon.Notify("文档导出成功！");
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Alert(msg);
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                }
            }
            catch
            {
                PageCommon.Notify("文档导出失败！");
                overlay.FadeOut();Cursor.Current = Cursors.Default;
            }
        }

        //打印-短租带驾
        private void btnPrintShortDriver_Click(object sender, EventArgs e)
        {
            try
            {
                ExcelHelper excelHelper = new ExcelHelper();
                string msg = "";
                if (excelHelper.PrintStatisticsByCustomerShortDriverList(dsAllShortDriver, ref msg))
                {
                    PageCommon.Notify("打印成功！");
                    overlay.FadeOut();Cursor.Current = Cursors.Default;
                }
                else
                {
                    PageCommon.Alert(msg);
                    overlay.FadeOut();Cursor.Current = Cursors.Default;
                }
            }
            catch
            {
                PageCommon.Notify("打印失败！");
                overlay.FadeOut();Cursor.Current = Cursors.Default;
            }
        }

        //返回-短租带驾
        private void btnCancelShortDriver_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 长租带驾

        //pagerLongDriver
        private int pagerLongDriver_EventPaging(EventPagingArg e)
        {
            return dgvBindLongDriver();
        }

        private int dgvBindLongDriver()
        {
            string strWhere = " Reserve.Type='3'  and Reserve.OrderSataus='true'";

            PageData pageData = new PageData(this, pagerLongDriver, this.overlay);

            pageData.TableName = "Reserve left join Customer on Customer.Id=Reserve.CustId left join Car on Car.Id=Reserve.CarId left join Accounts_Users on Accounts_Users.UserID=Reserve.UserId left join Accounts_Users DriverUser on DriverUser.UserId=Car.UserId";
            pageData.PrimaryKey = "Reserve.Id";
            pageData.OrderStr = "Reserve.InpuTime desc";
            pageData.PageIndex = this.pagerShortDriver.PageCurrent;
            pageData.PageSize = this.pagerShortDriver.PageSize;
            pageData.QueryCondition = strWhere + strSearchLongDriver.ToString();
            pageData.QueryFieldName = "DriverUser.TrueName as DriverName, Car.Code, Car.Brand, car.carType, car.TimeoutPrie, car.OvertimePrie , Reserve.ReceivedAmount-Reserve.TotalAmount as yingshou, Reserve.*,Customer.Name as customerName, Accounts_Users.TrueName as inputUserName";

            dgvInfoLongDriver.AutoGenerateColumns = false;
            DataSet ds = pageData.QueryDataTable();
            dgvInfoLongDriver.DataSource = this.pagerLongDriver.bindingSource;
            return pageData.TotalCount;
        }

        private void GetAllInfoLongDriverByCondistion()
        {
            //string _strSql = "1=1 ";
            string strWhere = "";
            strWhere = " where Reserve.Type='3'  and Reserve.OrderSataus='true'" + strSearchLongDriver;
            string strOrderBy = " order by Reserve.InpuTime desc";
            string strSelect = "select DriverUser.TrueName as DriverName, Car.Code, Car.Brand, car.carType, car.TimeoutPrie, car.OvertimePrie, Reserve.ReceivedAmount-Reserve.TotalAmount as yingshou , Reserve.*  ,Customer.Name as customerName, Accounts_Users.TrueName as inputUserName from Reserve left join Customer on Customer.Id=Reserve.CustId left join Car on Car.Id=Reserve.CarId left join Accounts_Users on Accounts_Users.UserID=Reserve.UserId left join Accounts_Users DriverUser on DriverUser.UserId=Car.UserId ";
            string strSql = strSelect + strWhere.ToString() + strOrderBy;

            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            CarSvc.SoapHeaderClass sopaHeader = new CarSvc.SoapHeaderClass();

            ServiceManipulator.SafeAsyncExecution<CarSvc.CarServiceSoapClient>(
                proxy => proxy.GetAllListForSearchCondistion(sopaHeader, strSql),
                result =>
                {
                    if (result.Error == null)
                    {
                        dsAllLongDriver = (DataSet)result.Result;
                        decimal sumShiShou = 0;
                        decimal sumWeiShou = 0;
                        decimal yingshou = 0;

                        if (dsAllLongDriver != null && dsAllLongDriver.Tables[0].Rows.Count > 0)
                        {
                            foreach (DataRow dr in dsAllLongDriver.Tables[0].Rows)
                            {
                                yingshou += Convert.ToDecimal(dr["yingshou"]);
                                sumShiShou += Convert.ToDecimal(dr["ReceivedAmount"]);
                                sumWeiShou += Convert.ToDecimal(dr["TotalAmount"]);
                            }
                        }
                        lblYingShouLongDriver.Text = yingshou.ToString() + " 元";
                        lblShiShouLongDriver.Text = sumShiShou.ToString() + " 元";
                        lblWeiShouLongDriver.Text = sumWeiShou.ToString() + " 元";
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message);
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );
        }

        private void dgvInfoLongDriver_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvInfoLongDriver.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvInfoLongDriver.Rows.Count; )
                {
                    this.dgvInfoLongDriver.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }

        private void dgvInfoLongDriver_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            e.CellStyle.WrapMode = DataGridViewTriState.True;

            if (this.dgvInfoLongDriver.Columns[e.ColumnIndex].Name == "日期LongDriver")
            {
                if (e.Value != null)
                {
                    e.Value = Convert.ToDateTime(e.Value).Date.ToString("yyyy-MM-dd");
                }
            }

            if (this.dgvInfoLongDriver.Columns[e.ColumnIndex].Name == "时间LongDriver")
            {
                if (e.Value != null)
                {
                    e.Value = Convert.ToDateTime(e.Value).Hour.ToString() + ":" + Convert.ToDateTime(e.Value).Minute.ToString();
                }
            }
            if (this.dgvInfoLongDriver.Columns[e.ColumnIndex].Name == "租期到期日")
            {
                if (e.Value != null)
                {
                    e.Value = Convert.ToDateTime(e.Value).ToString("yyyy-MM-dd");
                    this.dgvInfoLongDriver.Rows[e.RowIndex].Cells["租期到期时间"].Value = Convert.ToDateTime(e.Value).ToString("hh:mm:ss");
                }
            }
        }

        //查询-长租带驾
        private void btnSearchLongDriver_Click(object sender, EventArgs e)
        {
            string strWhere = "";
            strWhere += " and Reserve.InpuTime BETWEEN '" + dtstarttimeLongDrive.Value + "' AND '" + dtendtimeLongDrive.Value + "'";

            if (!string.IsNullOrEmpty(txtUserNameLongDriver.Text))
            {
                strWhere += " And Customer.Name like '%" + txtUserNameLongDriver.Text + "%'";
            }
            strSearchLongDriver = strWhere;

            this.pagerLongDriver.Bind();
            GetAllInfoLongDriverByCondistion();
        }

        //导出-长租带驾
        private void btnExportLongDriver_Click(object sender, EventArgs e)
        {
            try
            {
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string filePath = saveFileDialog.FileName;
                    ExcelHelper excelHelper = new ExcelHelper();
                    string msg = "";
                    if (excelHelper.StatisticsByCustomerLongDriverExport(dsAllLongDriver, filePath, ref msg))
                    {
                        PageCommon.Notify("文档导出成功！");
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Alert(msg);
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                }
            }
            catch
            {
                PageCommon.Notify("文档导出失败！");
                overlay.FadeOut();Cursor.Current = Cursors.Default;
            }
        }

        //打印-长租带驾
        private void btnPrintLongDriver_Click(object sender, EventArgs e)
        {
            try
            {
                ExcelHelper excelHelper = new ExcelHelper();
                string msg = "";
                if (excelHelper.PrintStatisticsByCustomerLongDriverList(dsAllLongDriver, ref msg))
                {
                    PageCommon.Notify("打印成功！");
                    overlay.FadeOut();Cursor.Current = Cursors.Default;
                }
                else
                {
                    PageCommon.Alert(msg);
                    overlay.FadeOut();Cursor.Current = Cursors.Default;
                }
            }
            catch
            {
                PageCommon.Notify("打印失败！");
                overlay.FadeOut();Cursor.Current = Cursors.Default;
            }
        }

        //返回-长租带驾
        private void btnCancelLongDriver_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region 短租自驾

        //pagerShortNotDriver
        private int pagerShortNotDriver_EventPaging(EventPagingArg e)
        {
            return dgvBindShortNotDriver();
        }

        private int dgvBindShortNotDriver()
        {
            string strWhere = " Reserve.Type='2'  and Reserve.OrderSataus='true'";

            PageData pageData = new PageData(this, pagerShortNotDriver, this.overlay);

            pageData.TableName = "Reserve left join Customer on Customer.Id=Reserve.CustId left join Car on Car.Id=Reserve.CarId left join Accounts_Users on Accounts_Users.UserID=Reserve.UserId left join Accounts_Users DriverUser on DriverUser.UserId=Car.UserId";
            pageData.PrimaryKey = "Reserve.Id";
            pageData.OrderStr = "Reserve.InpuTime desc";
            pageData.PageIndex = this.pagerShortDriver.PageCurrent;
            pageData.PageSize = this.pagerShortDriver.PageSize;
            pageData.QueryCondition = strWhere + strSearchShortNotDriver.ToString();
            pageData.QueryFieldName = "DriverUser.TrueName as DriverName, Car.Code, Car.Brand, car.carType, car.TimeoutPrie, car.OvertimePrie , Reserve.ReceivedAmount-Reserve.TotalAmount as yingshou, Reserve.*,Customer.Name as customerName, Accounts_Users.TrueName as inputUserName";

            dgvInfoShortNotDriver.AutoGenerateColumns = false;
            DataSet ds = pageData.QueryDataTable();
            dgvInfoShortNotDriver.DataSource = this.pagerShortNotDriver.bindingSource;
            return pageData.TotalCount;
        }

        private void GetAllInfoShortNotDriverByCondistion()
        {
            //string _strSql = "1=1 ";
            string strWhere = "";
            strWhere = " where Reserve.Type='2'  and Reserve.OrderSataus='true'" + strSearchShortNotDriver;
            string strOrderBy = " order by Reserve.InpuTime desc";
            string strSelect = "select DriverUser.TrueName as DriverName, Car.Code, Car.Brand, car.carType, car.TimeoutPrie, car.OvertimePrie , Reserve.ReceivedAmount-Reserve.TotalAmount as yingshou, Reserve.*  ,Customer.Name as customerName, Accounts_Users.TrueName as inputUserName from Reserve left join Customer on Customer.Id=Reserve.CustId left join Car on Car.Id=Reserve.CarId left join Accounts_Users on Accounts_Users.UserID=Reserve.UserId left join Accounts_Users DriverUser on DriverUser.UserId=Car.UserId ";
            string strSql = strSelect + strWhere.ToString() + strOrderBy;

            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            CarSvc.SoapHeaderClass sopaHeader = new CarSvc.SoapHeaderClass();

            ServiceManipulator.SafeAsyncExecution<CarSvc.CarServiceSoapClient>(
                proxy => proxy.GetAllListForSearchCondistion(sopaHeader, strSql),
                result =>
                {
                    if (result.Error == null)
                    {
                        dsAllShortNotDriver = (DataSet)result.Result;
                        decimal sumShiShou = 0;
                        decimal sumWeiShou = 0;
                        decimal yingshou = 0;

                        if (dsAllShortNotDriver != null && dsAllShortNotDriver.Tables[0].Rows.Count > 0)
                        {
                            foreach (DataRow dr in dsAllShortNotDriver.Tables[0].Rows)
                            {
                                yingshou += Convert.ToDecimal(dr["yingshou"]);
                                sumShiShou += Convert.ToDecimal(dr["ReceivedAmount"]);
                                sumWeiShou += Convert.ToDecimal(dr["TotalAmount"]);
                            }
                        }
                        lblYingShouShortNoDri.Text = yingshou.ToString() + " 元";
                        lblShiShouShortNoDri.Text = sumShiShou.ToString() + " 元";
                        lblWeiShouShortNotDri.Text = sumWeiShou.ToString() + " 元";
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message);
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );
        }

        private void dgvInfoShortNotDriver_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvInfoShortNotDriver.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvInfoShortNotDriver.Rows.Count; )
                {
                    this.dgvInfoShortNotDriver.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }

        private void dgvInfoShortNotDriver_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            e.CellStyle.WrapMode = DataGridViewTriState.True;
           
            if (this.dgvInfoShortNotDriver.Columns[e.ColumnIndex].Name == "日期ShortNotDri")
            {
                if (e.Value != null)
                {
                    e.Value = Convert.ToDateTime(e.Value).Date.ToString("yyyy-MM-dd");
                }
            }

            if (this.dgvInfoShortNotDriver.Columns[e.ColumnIndex].Name == "时间ShortNotDri")
            {
                if (e.Value != null)
                {
                    e.Value = Convert.ToDateTime(e.Value).Hour.ToString() + ":" + Convert.ToDateTime(e.Value).Minute.ToString();
                }
            }

            if (this.dgvInfoShortNotDriver.Columns[e.ColumnIndex].Name == "租期到期日ShortNotDri")
            {
                if (e.Value != null)
                {
                    e.Value = Convert.ToDateTime(e.Value).ToString("yyyy-MM-dd");
                    this.dgvInfoShortNotDriver.Rows[e.RowIndex].Cells["租期到期时间ShortNorDri"].Value = Convert.ToDateTime(e.Value).ToString("hh:mm:ss");
                }
            }
        }

        //查询-短租自驾
        private void btnSearchShortNotDriver_Click(object sender, EventArgs e)
        {
            string strWhere = "";
            strWhere += " and Reserve.InpuTime BETWEEN '" + dtstarttimeShortNotDrive.Value + "' AND '" + dtendtimeShortNotDrive.Value + "'";

            if (!string.IsNullOrEmpty(txtUserNameShortNotDriver.Text))
            {
                strWhere += " And Customer.Name like '%" + txtUserNameShortNotDriver.Text + "%'";
            }
            strSearchShortNotDriver = strWhere;

            this.pagerShortNotDriver.Bind();
            GetAllInfoShortNotDriverByCondistion();
        }

        //导出-短租自驾
        private void btnExportShortNotDriver_Click(object sender, EventArgs e)
        {
            try
            {
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string filePath = saveFileDialog.FileName;
                    ExcelHelper excelHelper = new ExcelHelper();
                    string msg = "";
                    if (excelHelper.StatisticsByCustomerShortNotDriverExport(dsAllShortNotDriver, filePath, ref msg))
                    {
                        PageCommon.Notify("文档导出成功！");
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Alert(msg);
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                }
            }
            catch
            {
                PageCommon.Notify("文档导出失败！");
                overlay.FadeOut();Cursor.Current = Cursors.Default;
            }
        }

        //打印-短租自驾
        private void btnPrintShortNotDriver_Click(object sender, EventArgs e)
        {
            try
            {
                ExcelHelper excelHelper = new ExcelHelper();
                string msg = "";
                if (excelHelper.PrintStatisticsByCustomerShortNotDriverList(dsAllShortNotDriver, ref msg))
                {
                    PageCommon.Notify("打印成功！");
                    overlay.FadeOut();Cursor.Current = Cursors.Default;
                }
                else
                {
                    PageCommon.Alert(msg);
                    overlay.FadeOut();Cursor.Current = Cursors.Default;
                }
            }
            catch
            {
                PageCommon.Notify("打印失败！");
                overlay.FadeOut();Cursor.Current = Cursors.Default;
            }
        }

        //返回-短租自驾
        private void btnCancelShortNotDriver_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region 长租自驾
        //pagerLongNotDriver
        private int pagerLongNotDriver_EventPaging(EventPagingArg e)
        {
            return dgvBindLongNotDriver();
        }

        private int dgvBindLongNotDriver()
        {
            string strWhere = " Reserve.Type='1' and Reserve.OrderSataus='true' ";

            PageData pageData = new PageData(this, pagerLongNotDriver, this.overlay);

            pageData.TableName = "Reserve left join Customer on Customer.Id=Reserve.CustId left join Car on Car.Id=Reserve.CarId left join Accounts_Users on Accounts_Users.UserID=Reserve.UserId left join Accounts_Users DriverUser on DriverUser.UserId=Car.UserId";
            pageData.PrimaryKey = "Reserve.Id";
            pageData.OrderStr = "Reserve.InpuTime desc";
            pageData.PageIndex = this.pagerShortDriver.PageCurrent;
            pageData.PageSize = this.pagerShortDriver.PageSize;
            pageData.QueryCondition = strWhere + strSearchLongNotDriver.ToString();
            pageData.QueryFieldName = "DriverUser.TrueName as DriverName, Car.Code, Car.Brand, car.carType, car.TimeoutPrie, car.OvertimePrie, Reserve.ReceivedAmount-Reserve.TotalAmount as yingshou , Reserve.*,Customer.Name as customerName, Accounts_Users.TrueName as inputUserName";

            dgvInfoLongNotDriver.AutoGenerateColumns = false;
            DataSet ds = pageData.QueryDataTable();
            dgvInfoLongNotDriver.DataSource = this.pagerLongNotDriver.bindingSource;
            return pageData.TotalCount;
        }

        private void GetAllInfoLongNotDriverByCondistion()
        {
            //string _strSql = "1=1 ";
            string strWhere = "";
            strWhere = " where Reserve.Type='1' and Reserve.OrderSataus='true' " + strSearchLongNotDriver;
            string strOrderBy = " order by Reserve.InpuTime desc";
            string strSelect = "select DriverUser.TrueName as DriverName, Car.Code, Car.Brand, car.carType, car.TimeoutPrie, car.OvertimePrie , Reserve.ReceivedAmount-Reserve.TotalAmount as yingshou, Reserve.*  ,Customer.Name as customerName, Accounts_Users.TrueName as inputUserName from Reserve left join Customer on Customer.Id=Reserve.CustId left join Car on Car.Id=Reserve.CarId left join Accounts_Users on Accounts_Users.UserID=Reserve.UserId left join Accounts_Users DriverUser on DriverUser.UserId=Car.UserId ";
            string strSql = strSelect + strWhere.ToString() + strOrderBy;

            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            CarSvc.SoapHeaderClass sopaHeader = new CarSvc.SoapHeaderClass();

            ServiceManipulator.SafeAsyncExecution<CarSvc.CarServiceSoapClient>(
                proxy => proxy.GetAllListForSearchCondistion(sopaHeader, strSql),
                result =>
                {
                    if (result.Error == null)
                    {
                        dsAllLongNotDriver = (DataSet)result.Result;
                        decimal sumShiShou = 0;
                        decimal sumWeiShou = 0;
                        decimal yingshou = 0;

                        if (dsAllLongNotDriver != null && dsAllLongNotDriver.Tables[0].Rows.Count > 0)
                        {
                            foreach (DataRow dr in dsAllLongNotDriver.Tables[0].Rows)
                            {
                                yingshou += Convert.ToDecimal(dr["yingshou"]);
                                sumShiShou += Convert.ToDecimal(dr["ReceivedAmount"]);
                                sumWeiShou += Convert.ToDecimal(dr["TotalAmount"]);
                            }
                        }
                        lblYingShouLongNotDri.Text = yingshou.ToString() + " 元";
                        lblShiShouLongNotDri.Text = sumShiShou.ToString() + " 元";
                        lblWeiShouLongNotDri.Text = sumWeiShou.ToString() + " 元";
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message);
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );
        }

        private void dgvInfoLongNotDriver_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvInfoLongNotDriver.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvInfoLongNotDriver.Rows.Count; )
                {
                    this.dgvInfoLongNotDriver.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }

        private void dgvInfoLongNotDriver_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            e.CellStyle.WrapMode = DataGridViewTriState.True;
            //int time = 0;
            //decimal dayrent = 0;
            //decimal Rent = 0;

            //dayrent = Convert.ToDecimal(this.dgvInfoLongNotDriver.Rows[e.RowIndex].Cells["每日租金LongNotDri"].Value);
            //time = Convert.ToInt32(this.dgvInfoLongNotDriver.Rows[e.RowIndex].Cells["租赁时间LongNotDri"].Value);
            //this.dgvInfoLongNotDriver.Rows[e.RowIndex].Cells["应收金额LongNotDri"].Value = time * dayrent;
            //Rent = Convert.ToInt32(this.dgvInfoLongNotDriver.Rows[e.RowIndex].Cells["实收金额LongNotDri"].Value);
            //this.dgvInfoLongNotDriver.Rows[e.RowIndex].Cells["未收账款LongNotDri"].Value = time * dayrent - Rent;

            if (this.dgvInfoLongNotDriver.Columns[e.ColumnIndex].Name == "日期LongNotDri")
            {
                if (e.Value != null)
                {
                    e.Value = Convert.ToDateTime(e.Value).Date.ToString("yyyy-MM-dd");
                }
            }

            if (this.dgvInfoLongNotDriver.Columns[e.ColumnIndex].Name == "时间LongNotDri")
            {
                if (e.Value != null)
                {
                    e.Value = Convert.ToDateTime(e.Value).Hour.ToString() + ":" + Convert.ToDateTime(e.Value).Minute.ToString();
                }
            }

            if (this.dgvInfoLongNotDriver.Columns[e.ColumnIndex].Name == "租期到期日LongNotDri")
            {
                if (e.Value != null)
                {
                    e.Value = Convert.ToDateTime(e.Value).ToString("yyyy-MM-dd");
                    this.dgvInfoLongNotDriver.Rows[e.RowIndex].Cells["租期到期时间LongNotDri"].Value = Convert.ToDateTime(e.Value).ToString("hh:mm:ss");
                }
            }
        }

        //查询-长租自驾
        private void btnSearchLongNotDriver_Click(object sender, EventArgs e)
        {
            string strWhere = "";
            strWhere += " and Reserve.InpuTime BETWEEN '" + dtstarttimeLongNotDrive.Value + "' AND '" + dtendtimeLongNotDrive.Value + "'";

            if (!string.IsNullOrEmpty(txtUserNameLongNotDriver.Text))
            {
                strWhere += " And Customer.Name like '%" + txtUserNameLongNotDriver.Text + "%'";
            }

            strSearchLongNotDriver = strWhere;
            this.pagerLongNotDriver.Bind();
            GetAllInfoLongNotDriverByCondistion();

        }

        //导出-长租自驾
        private void btnExportLongNotDriver_Click(object sender, EventArgs e)
        {
            try
            {
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string filePath = saveFileDialog.FileName;
                    ExcelHelper excelHelper = new ExcelHelper();
                    string msg = "";
                    if (excelHelper.StatisticsByCustomerLongNotDriverExport(dsAllLongNotDriver, filePath, ref msg))
                    {
                        PageCommon.Notify("文档导出成功！");
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Alert(msg);
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                }
            }
            catch
            {
                PageCommon.Notify("文档导出失败！");
                overlay.FadeOut();Cursor.Current = Cursors.Default;
            }
        }

        //打印-长租自驾
        private void btnPrintLongNotDriver_Click(object sender, EventArgs e)
        {
            try
            {
                ExcelHelper excelHelper = new ExcelHelper();
                string msg = "";
                if (excelHelper.PrintStatisticsByCustomerLongNotDriverList(dsAllLongNotDriver, ref msg))
                {
                    PageCommon.Notify("打印成功！");
                    overlay.FadeOut();Cursor.Current = Cursors.Default;
                }
                else
                {
                    PageCommon.Alert(msg);
                    overlay.FadeOut();Cursor.Current = Cursors.Default;
                }
            }
            catch
            {
                PageCommon.Notify("打印失败！");
                overlay.FadeOut();Cursor.Current = Cursors.Default;
            }
        }

        //返回-长租自驾
        private void btnCancelLongNotDriver_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        TextBox clickTextBox = new TextBox();
        string customerName = "";

        private void txtUserNameShortDriver_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string strSearch = txtUserNameShortDriver.Text;
                if (string.IsNullOrEmpty(strSearch))
                {
                    DialogWarning dia = new DialogWarning("客户名不能为空！");
                    dia.ShowDialog();
                    return;
                }
                else
                {
                    clickTextBox = txtUserNameShortDriver;
                    DisplayCustomeLIst(strSearch, "姓名");
                }
            }
        }

        private void DisplayCustomeLIst(string strSearch, string type)
        {
            if (string.IsNullOrEmpty(strSearch))
            {
                return;
            }

            overlay.FadeIn(); Cursor.Current = Cursors.WaitCursor;
            string strSelectWhere = "";
            if (type == "姓名")
            {
                strSelectWhere = " Name like '%" + strSearch + "%'";
            }
            if (type == "客户编号")
            {
                strSelectWhere = " Code like '%" + strSearch + "%'";
            }
            DataSet ds = null;
            dgvInfo.DataSource = null;
            dgvInfo.AutoGenerateColumns = false;

            CustomerSvc.SoapHeaderClass sopaHeader = new Aliyeye.Car.Net.UI.CustomerSvc.SoapHeaderClass();
            ServiceManipulator.SafeAsyncExecution<CustomerSvc.CustomerServiceSoapClient>(
                proxy => proxy.GetList(sopaHeader, strSelectWhere),
                result =>
                {
                    if (result.Error == null)
                    {
                        ds = (DataSet)result.Result;
                        if (ds != null && ds.Tables[0].Rows.Count > 0)
                        {
                            dgvInfo.DataSource = ds.Tables[0];
                            this.dgvInfo.Visible = true;
                            this.dgvInfo.Focus();
                        }
                        else
                        {
                            clickTextBox.Text = "";
                            PageCommon.Alert("查找的客户不存在，请重新查找！");
                        }
                        overlay.FadeOut(); Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut(); Cursor.Current = Cursors.Default;
                    }
                },
                this
            );

        }

        private void dgvInfo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                clickTextBox.Text = this.dgvInfo.CurrentRow.Cells["姓名"].Value.ToString();
                customerName = this.clickTextBox.Text;
                this.dgvInfo.Visible = false;
            }
        }

        private void dgvInfo_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvInfo.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvInfo.Rows.Count; )
                {
                    this.dgvInfo.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }

        private void dgvInfo_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            e.CellStyle.WrapMode = DataGridViewTriState.True;


            if (this.dgvInfo.Columns[e.ColumnIndex].Name == "客户类型")
            {
                if (e.Value != null)
                {
                    if (e.Value.ToString().Trim() == "1")
                    {
                        e.Value = "个体户客户";

                    }
                    else
                    {
                        e.Value = "企业客户";
                    }
                }
            }
        }

        private void txtUserNameLongDriver_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string strSearch = txtUserNameLongDriver.Text;
                if (string.IsNullOrEmpty(strSearch))
                {
                    DialogWarning dia = new DialogWarning("客户名不能为空！");
                    dia.ShowDialog();
                    return;
                }
                else
                {
                    clickTextBox = txtUserNameLongDriver;
                    DisplayCustomeLIst(strSearch, "姓名");
                }
            }
        }

        private void txtUserNameShortNotDriver_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string strSearch = txtUserNameShortNotDriver.Text;
                if (string.IsNullOrEmpty(strSearch))
                {
                    DialogWarning dia = new DialogWarning("客户名不能为空！");
                    dia.ShowDialog();
                    return;
                }
                else
                {
                    clickTextBox = txtUserNameShortNotDriver;
                    DisplayCustomeLIst(strSearch, "姓名");
                }
            }
        }

        private void txtUserNameLongNotDriver_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string strSearch = txtUserNameLongNotDriver.Text;
                if (string.IsNullOrEmpty(strSearch))
                {
                    DialogWarning dia = new DialogWarning("客户名不能为空！");
                    dia.ShowDialog();
                    return;
                }
                else
                {
                    clickTextBox = txtUserNameLongNotDriver;
                    DisplayCustomeLIst(strSearch, "姓名");
                }
            }
        }

        //private void tabList_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    switch (this.tabList.SelectedIndex)
        //    {
        //        case 0:

        //            break;
        //        case 1:
        //            if (dsAllLongDriver == null)
        //            {
        //                this.pagerLongDriver.PageCurrent = 1;
        //                this.pagerLongDriver.Bind();
        //                GetAllInfoLongDriverByCondistion();
        //            }
        //            break;
        //        case 2:
        //            if (dsAllShortNotDriver == null)
        //            {
        //                this.pagerShortNotDriver.PageCurrent = 1;
        //                this.pagerShortNotDriver.Bind();
        //                GetAllInfoShortNotDriverByCondistion();
        //            }
        //            break;
        //        case 3:
        //            if (dsAllLongNotDriver == null)
        //            {
        //                this.pagerLongNotDriver.PageCurrent = 1;
        //                this.pagerLongNotDriver.Bind();
        //                GetAllInfoLongNotDriverByCondistion();
        //            }
        //            break;
        //    }
        //}


    }
}
